/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pt.iasd.youthshelter.pathfinder.wizards;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

public final class ChurchVisualPanel extends JPanel implements DocumentListener, ActionListener {
    private static final ResourceBundle bundle = java.util.ResourceBundle.getBundle("pt/iasd/youthshelter/pathfinder/Bundle");
    
    /**
     * Creates new form ChurchVisualPanel
     */
    public ChurchVisualPanel() {
        this(null, null, null, null);
    }
    
    public ChurchVisualPanel(String name, String city, String region, String country) {
        initComponents();
        boolean editionMode = false;
        if(name != null && !name.isEmpty()) {
            this.NameTextField.setText(name);
            editionMode = true;
        }
        if(city != null && !city.isEmpty()) {
            this.CityTextField.setText(city);
            editionMode = true;
        }
        if(region != null && !region.isEmpty()) {
            this.RegionTextField.setText(region);
            editionMode = true;
        }
        if(country != null && !country.isEmpty()) {
            this.CountryComboBox.setSelectedItem(new Locale(country));
            editionMode = true;
        }
        if(editionMode) {
            changedUpdate(null);
        }
    }

    @Override
    public String getName() {
        return WizardKeys.PROP_CHURCH_STEPS[0];
    }

    public JTextField getCityTextField() {
        return CityTextField;
    }

    public JTextField getNameTextField() {
        return NameTextField;
    }

    public JTextField getRegionTextField() {
        return RegionTextField;
    }

    public JComboBox getCountryComboBox() {
        return CountryComboBox;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        NameLabel = new javax.swing.JLabel();
        NameTextField = new javax.swing.JTextField();
        CityTextField = new javax.swing.JTextField();
        CityLabel = new javax.swing.JLabel();
        RegionTextField = new javax.swing.JTextField();
        RegionLabel = new javax.swing.JLabel();
        CountryLabel = new javax.swing.JLabel();
        CountryComboBox = new javax.swing.JComboBox();

        NameLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(NameLabel, bundle.getString("ChurchVisualPanel.NameLabel.text"));
        NameLabel.setMaximumSize(new java.awt.Dimension(43, 14));
        NameLabel.setMinimumSize(new java.awt.Dimension(43, 14));
        NameLabel.setPreferredSize(new java.awt.Dimension(43, 14));

        CityLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(CityLabel, bundle.getString("ChurchVisualPanel.CityLabel.text"));
        CityLabel.setMaximumSize(new java.awt.Dimension(43, 14));
        CityLabel.setMinimumSize(new java.awt.Dimension(43, 14));
        CityLabel.setPreferredSize(new java.awt.Dimension(43, 14));

        RegionLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(RegionLabel, bundle.getString("ChurchVisualPanel.RegionLabel.text"));
        RegionLabel.setMaximumSize(new java.awt.Dimension(43, 14));
        RegionLabel.setMinimumSize(new java.awt.Dimension(43, 14));
        RegionLabel.setPreferredSize(new java.awt.Dimension(43, 14));

        CountryLabel.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        org.openide.awt.Mnemonics.setLocalizedText(CountryLabel, bundle.getString("ChurchVisualPanel.CountryLabel.text"));

        CountryComboBox.setModel(new DefaultComboBoxModel(buildCountryComboBoxData()));
        CountryComboBox.setPreferredSize(new java.awt.Dimension(175, 20));
        CountryComboBox.addActionListener(this);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(CountryLabel)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(CountryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 135, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(RegionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(CityLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(CityTextField)
                            .addComponent(RegionTextField)
                            .addComponent(NameTextField, javax.swing.GroupLayout.Alignment.TRAILING))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(NameLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(NameTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CityTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(CityLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(RegionTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(RegionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(CountryLabel)
                    .addComponent(CountryComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        NameTextField.getDocument().addDocumentListener(this);
        CityTextField.getDocument().addDocumentListener(this);
        RegionTextField.getDocument().addDocumentListener(this);
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel CityLabel;
    private javax.swing.JTextField CityTextField;
    private javax.swing.JComboBox CountryComboBox;
    private javax.swing.JLabel CountryLabel;
    private javax.swing.JLabel NameLabel;
    private javax.swing.JTextField NameTextField;
    private javax.swing.JLabel RegionLabel;
    private javax.swing.JTextField RegionTextField;
    // End of variables declaration//GEN-END:variables

    @Override
    public void insertUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
        changedUpdate(e);
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }
    
    // <editor-fold defaultstate="collapsed" desc="ComboBox Data Loading Code">
    private String[] buildCountryComboBoxData() {
        List<String> countries = new ArrayList<String>();
        Locale[] locales = Locale.getAvailableLocales();
        for (Locale locale : locales) {
            String name = locale.getDisplayCountry();
            if(!name.isEmpty()) {
                countries.add(name);
            }
        }
        Collections.sort(countries);
        return countries.toArray(new String[countries.size()]);
    }// </editor-fold>

    @Override
    public void actionPerformed(ActionEvent e) {
        firePropertyChange(WizardKeys.PROP_ANY, 0, 1);
    }
}
